package com.sqstudio.view
{
	import com.sqstudio.common.DataProxy;
	import com.sqstudio.common.LunarCalendar;
	import com.sqstudio.ui.CountMc;
	
	import flash.display.Sprite;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	
	public class CountPanel extends Sprite
	{
		private var _countMC:CountMc;
		public function CountPanel()
		{
			this._countMC = new CountMc();
			addChild(this._countMC);
			
			var txtArr:Array = [_countMC.d11,_countMC.d12,_countMC.d13,_countMC.d21,_countMC.m11,_countMC.z11,
				_countMC.s11,_countMC.y21,_countMC.m21,_countMC.x21];
			
			for each(var txt:TextField in txtArr){
				txt.mouseEnabled = txt.selectable = false;
			}
			
			var txt2Arr:Array = [_countMC.y1,_countMC.y2,_countMC.m1,_countMC.m2,_countMC.y3,_countMC.m3,_countMC.d1,_countMC.d2,_countMC.d3,_countMC.days];
			for each(var txt2:TextField in txt2Arr){
				txt2.restrict = "0-9";
				txt2.addEventListener(FocusEvent.FOCUS_IN,inHandler);
				txt2.addEventListener(FocusEvent.FOCUS_OUT,outHandler);
			}
			
			this._countMC.totalBtn.addEventListener(MouseEvent.CLICK,totalCKHandler);
			this._countMC.dateIsBtn.addEventListener(MouseEvent.CLICK,dateHandler);
			
			
			_countMC.d11.text = "";
			_countMC.m11.text = "";
			_countMC.d12.text = "";
			_countMC.z11.text = "";
			_countMC.d13.text = "";
			_countMC.s11.text = "";
			_countMC.desc.text = "";
			_countMC.d21.text = "";
			_countMC.m21.text = "";
			_countMC.y21.text = "";
			_countMC.x21.text = "";
		}
		
		private function inHandler(e:FocusEvent):void
		{
			var txt:TextField = e.currentTarget as TextField;
			txt.filters = [new GlowFilter(0xE35491)];
		}
		
		private function outHandler(e:FocusEvent):void
		{
			var txt:TextField = e.currentTarget as TextField;
			txt.filters = [];
		}
		
		private function totalCKHandler(event:MouseEvent):void
		{
			
			_countMC.d11.text = "";
			_countMC.m11.text = "";
			_countMC.d12.text = "";
			_countMC.z11.text = "";
			_countMC.d13.text = "";
			_countMC.s11.text = "";
			_countMC.desc.text = "";
			
			if(int(_countMC.y1.text)>=1000 &&  int(_countMC.y2.text)>=1000){
				if(int(_countMC.m1.text)>0 && int(_countMC.m1.text)<13 && int(_countMC.m2.text)>0 && int(_countMC.m2.text)<13){
					if(int(_countMC.d1.text)>0 && int(_countMC.d1.text)<31 && int(_countMC.d2.text)>0 && int(_countMC.d2.text)<31){
						var sDate:Date = new Date(int(_countMC.y1.text),int(_countMC.m1.text)-1,int(_countMC.d1.text));
						var eDate:Date = new Date(int(_countMC.y2.text),int(_countMC.m2.text)-1,int(_countMC.d2.text));
						var days:int = DataProxy.getDaysBetweenDates(sDate,eDate);
						_countMC.d11.text = String(days);
						
						_countMC.m11.text = String(int(days/30));
						_countMC.d12.text = String(days%30);
						
						_countMC.z11.text = String(int(days/7));
						_countMC.d13.text = String(int(days%7));
						
						_countMC.s11.text = String(days*24);
					}else{
						_countMC.desc.text = "请确保天数在1和31之间！";
					}
				}else{
					_countMC.desc.text = "请确保月份在1和12之间！";
				}
			}else{
				_countMC.desc.text = "请确保年份为四位数！";
			}
			
		}
		private function dateHandler(event:MouseEvent):void
		{
			
			_countMC.d21.text = "";
			_countMC.m21.text = "";
			_countMC.y21.text = "";
			_countMC.x21.text = "";
			_countMC.desc.text = "";
			
			if(int(_countMC.y3.text)>=1000){
				if(int(_countMC.m3.text)>0 && int(_countMC.m3.text)<13 ){
					if(int(_countMC.d3.text)>0 && int(_countMC.d3.text)<31){
						var sDate:Date = new Date(int(_countMC.y3.text),int(_countMC.m3.text)-1,int(_countMC.d3.text));
						sDate.setDate(sDate.date+int(_countMC.days.text));
						
						_countMC.y21.text = String(sDate.getFullYear());
						_countMC.m21.text = String(sDate.month+1);
						_countMC.d21.text = String(sDate.date);
						_countMC.x21.text = "星期"+new LunarCalendar(sDate).getGrDay();
					}else{
						_countMC.desc.text = "请确保天数在1和31之间！";
					}
				}else{
					_countMC.desc.text = "请确保月份在1和12之间！";
				}
			}else{
				_countMC.desc.text = "请确保年份为四位数！";
			}
			
		}
	}
}